package com.xxx.lib.base

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ktx.immersionBar
import com.xxx.lib.R
import org.greenrobot.eventbus.EventBus

/**
 * Activity一级基类
 */
abstract class BaseActivity : AppCompatActivity(), IBaseView {

    /**
     * 初始化布局
     */
    abstract fun initContentView(): View?

    /**
     * 初始化操作
     */
    abstract fun init()

    /**
     * 是否初始化
     */
    private var isInited = true

    /**
     * 通用加载框
     */
    private val alertDialog: AlertDialog by lazy {
        AlertDialog.Builder(this).setView(R.layout.dialog_loading).create().apply {
            setCanceledOnTouchOutside(false)
            window?.setBackgroundDrawable(ColorDrawable())
            window?.setDimAmount(0F)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            fitsSystemWindows(true)
            statusBarDarkFont(true)
            statusBarColor(android.R.color.white)
            navigationBarColor(android.R.color.white)
        }
        val contentView = initContentView()
        if (contentView != null) {
            setContentView(contentView)
            setSupportActionBar(findViewById(R.id.toolBar))//绑定标题栏
            supportActionBar?.setDisplayShowTitleEnabled(false)//不展示原生标题
            supportActionBar?.setDisplayHomeAsUpEnabled(true)//展示左侧按钮
        }
    }

    override fun onResume() {
        super.onResume()
        if (isInited) {
            isInited = false
            init()
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        return if (!super.onSupportNavigateUp()) {
            onBackPressed()
            false
        } else {
            true
        }
    }


    /**
     * 设置标题
     * @param title 标题
     */
    override fun setTitle(title: CharSequence) {
        findViewById<TextView>(R.id.tvToolBarTitle)?.text = title
    }

    /**
     * 显示加载框
     * @param hint 提示语，可不传
     */
    override fun showLoadingDialog(hint: String) {
        if (isFinishing) return
        runOnUiThread {
            try {
                if (!alertDialog.isShowing) alertDialog.show()
                setLoadingDialogHint(hint)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 隐藏加载框
     */
    override fun dismissLoadingDialog() {
        if (isFinishing) return
        runOnUiThread {
            try {
                alertDialog.dismiss()
                setLoadingDialogHint("")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 设置加载框提示
     * @param hint 提示语
     */
    private fun setLoadingDialogHint(hint: String) {
        if (isFinishing) return
        runOnUiThread {
            try {
                alertDialog.findViewById<TextView>(R.id.tvHint)?.apply {
                    visibility = if (hint.isBlank()) View.GONE else View.VISIBLE
                    text = hint
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 注册EventBus
     */
    protected fun registerEventBus() {
        EventBus.getDefault().run {
            if (!isRegistered(this@BaseActivity)) register(this@BaseActivity)
        }
    }

    /**
     * 注销EventBus
     */
    private fun unregisterEventBus() {
        EventBus.getDefault().run {
            if (isRegistered(this@BaseActivity)) unregister(this@BaseActivity)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
        dismissLoadingDialog()
    }
}
